package com.rocching.easychat.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.rocching.easychat.MainActivity;
import com.rocching.easychat.R;
import com.rocching.easychat.adapter.NewFriendListAdapter;
import com.rocching.easychat.core.IApiCallback;
import com.rocching.easychat.model.AgreeUserListModel;
import com.rocching.easychat.model.AgreeUserModel;
import com.rocching.easychat.model.ResponseModel;
import com.rocching.easychat.service.FriendService;
import com.rocching.easychat.service.dto.BeFriendInput;

import java.util.Objects;

public class NewFriendActivity extends AppCompatActivity {
    private final String TAG = NewFriendActivity.class.getName();
    private NewFriendListAdapter newFriendListAdapter;
    private boolean needRefresh = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_friend);

        init();
    }

    @Override
    public void onBackPressed() {
        Log.i(TAG, "the needRefresh is:" + needRefresh);

        if (needRefresh) {
            Intent intent = new Intent();
            intent.putExtra("needRefresh", needRefresh);
            this.setResult(RESULT_OK, intent);
        }
        finish();
    }

    private void init() {
        NewFriendListAdapter.BtnClickListener clickListener = new NewFriendListAdapter.BtnClickListener() {
            @Override
            public void onClick(int action, AgreeUserModel model) {
                handleClick(action, model.getId());
            }
        };

        ListView listView = findViewById(R.id.friend_list);
        newFriendListAdapter = new NewFriendListAdapter(this, clickListener);
        listView.setAdapter(newFriendListAdapter);

        initData();
    }

    private void initData() {
        FriendService.getInstance().getAgreeFriendList(new IApiCallback<AgreeUserListModel>() {
            @Override
            public void onResponse(boolean succeeded, String body, AgreeUserListModel data) {
                if (succeeded) {
                    updateView(data);
                }
            }
        });
    }

    private void updateView(AgreeUserListModel data) {
        if (Objects.isNull(data)) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                newFriendListAdapter.update(data.getList());
            }
        });
    }

    private void handleClick(int action, long friendId) {
        BeFriendInput input = new BeFriendInput();
        input.setAction(action);
        input.setFriendId(friendId);
        input.setGreetMsg("");
        FriendService.getInstance().beFriend(input, new IApiCallback<ResponseModel>() {
            @Override
            public void onResponse(boolean succeeded, String body, ResponseModel data) {
                if (succeeded) {
                    if (data.getFlag()) {
                        needRefresh = true;
                        initData();
                    } else {
                        showText(data.getMsg());
                    }
                }
            }
        });
    }

    private void showText(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}